import { _decorator, Component, instantiate, Node, Prefab, tween, Vec3 } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
import ResManager from '../common/ResManager';
import { Village } from '../entity/Village';
import { VillageItem } from './VillageItem';
import { ChangeVillageItem } from './ChangeVillageItem';
const { ccclass, property } = _decorator;

@ccclass('ChangeVillage')
export class ChangeVillage extends Component {
    @property(Node)
    mask:Node;
    @property(Node)
    close:Node;
    @property(Node)
    content:Node;
    start() {
        this.mask.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)

        this.renderVillageList()
    }

    async renderVillageList(){
        let villageObjList = UserInfo.instance.villageData.villageObjList

        let list = villageObjList.filter(v => {
            return v.zoneId != UserInfo.instance.villageData.curVid
        })

        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>(PrefabEnum.ChangeVillageItem)
        for(let obj of list){
            let village = Village.createByVillageObj(obj)
            let node = instantiate(prefab)
            this.content.addChild(node)
            node.getComponent(ChangeVillageItem).setData(village)
        }
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.ChangeVillage)
    }
}


